<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 * @version		$Id:$
 */
?>
<?php if (isset($this->controllers) && $this->controllers) : ?>
<?php foreach ($this->controllers as $index => $controller) : ?>
<?php if ($index > 0 && $index % 3 == 0) : ?>
<div class="clear"></div>
<?php endif; ?>
<div class="col w33">
	<div class="content">
		<div class="box header mb0">
			<div class="head"><div></div></div>
			<h2 class="g_a_breadcump">
				<a class="fl" href="javascript: void(0);"><?php echo $this->TRANSLATOR->translator($controller['controller']['description'], $controller['controller']['module_name']); ?></a>
				
				<?php if (in_array($controller['controller']['controller_name'], $this->installedControllers)) : ?>
					<?php if ($this->ACL->isAllowed('delete', 'controller', 'core')) : ?>
					<a class="fr g_a_controllers delete" title="<?php echo $this->TRANSLATOR->translator('common_button_delete'); ?>" href="javascript: void(0);" rel="delete_<?php echo $controller['controller']['module_name'] . '_' . $controller['controller']['controller_name']; ?>"></a>
					<?php else : ?>
					<div class="fr green g_a_controllers_installed"><?php echo $this->TRANSLATOR->translator('common_installed'); ?></div>
					<?php endif; ?>
				<?php else : ?>
					<?php if ($this->ACL->isAllowed('add', 'controller', 'core')) : ?>
					<a class="fr g_a_controllers add" title="<?php echo $this->TRANSLATOR->translator('common_button_add'); ?>" href="javascript: void(0);" rel="add_<?php echo $controller['controller']['module_name'] . '_' . $controller['controller']['controller_name']; ?>"></a>
					<?php else : ?>
					<div class="fr red g_a_controllers_uninstalled"><?php echo $this->TRANSLATOR->translator('common_uninstalled'); ?></div>
					<?php endif; ?>
				<?php endif; ?>
				<div class="clear"></div>
			</h2>
		</div>
	</div>
	<div class="content">
		<div class="desc">
			<div class="body">
				<?php if ($controller['actions']) : ?>
				<?php $dbActions = Modules_Core_Services_Action::dbActions($controller['controller']['module_name'], $controller['controller']['controller_name'], true); ?>
				<ul>
					<?php foreach ($controller['actions'] as $action) : ?>
					<li>
						<p class="fl col w8"><?php echo $this->TRANSLATOR->translator($action['description'], $action['module_name']); ?></p>
						<p class="fr">
							<?php if (in_array($action['name'], $dbActions)) : ?>
								<?php if ($this->ACL->isAllowed('delete', 'action', 'core')) : ?>
								<a class="g_a_controllers_actions delete" rel="delete_<?php echo $controller['controller']['module_name'] . '_' . $controller['controller']['controller_name'] . '_' . $action['name']; ?>" title="<?php echo $this->TRANSLATOR->translator('common_button_delete'); ?>" href="javascript: void(0);"></a>
								<?php else : ?>
								<div class="fr green g_a_actions_installed"><?php echo $this->TRANSLATOR->translator('common_installed'); ?></div>
								<?php endif; ?>
							<?php else : ?>
								<?php if ($this->ACL->isAllowed('add', 'action', 'core')) : ?>
								<a class="g_a_controllers_actions add" rel="add_<?php echo $controller['controller']['module_name'] . '_' . $controller['controller']['controller_name'] . '_' . $action['name']; ?>" title="<?php echo $this->TRANSLATOR->translator('common_button_add'); ?>" href="javascript: void(0);"></a>
								<?php else : ?>
								<div class="fr red g_a_actions_uninstalled"><?php echo $this->TRANSLATOR->translator('common_uninstalled'); ?></div>
								<?php endif; ?>
							<?php endif; ?>
						</p>
						<div class="clear"></div>
					</li>
					<?php endforeach; ?>
				</ul>
				<?php endif; ?>
			</div>
		</div>
		<div class="body_footer">
			<div class="bottom_left"><div class="bottom_right"></div></div>
		</div>
	</div>
</div>
<?php endforeach; ?>
<script type="text/javascript">
'Gio.Core.Controller'.namespace();
'Gio.Core.Action'.namespace();
Gio.Core.Controller.currenAction = null;
Gio.Core.Action.currenAction = null;
Gio.Core.Controller.actions = function(params) {
	/**
	 * Show g_a_ajax_loading element
	 */
	$('#g_a_ajax_loading').show();
	$.ajaxq('core_controller_action', {
		type: 'post',
		url: params.url,
		data: params,
		success: function(response) {
			if (response == 'RESULT_OK') {
				switch (params.act) {
					case 'add':
						var rel = 'delete_' + params.module_id  + '_' + params.controller_id;
						var message = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('controller_actions_add_success')); ?>';
						var className = 'delete';
						break;
					case 'delete':
						var rel = 'add_' + params.module_id  + '_' + params.controller_id;
						var message = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('controller_actions_delete_success')); ?>';
						var className = 'add';
						$(Gio.Core.Controller.currenAction).parents('div.col').find('.delete').removeClass('delete').addClass('add');
						break;
				}
				$(Gio.Core.Controller.currenAction).attr('rel', rel)
												   .removeClass('delete')
												   .removeClass('add')
												   .addClass(className);
				Gio.Message.success(message);
			} else {
				Gio.Message.error('<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_error_message')); ?>');
			}
			/**
			 * Hide g_a_ajax_loading element
			 */
			$('#g_a_ajax_loading').hide();
		}
	});
};

Gio.Core.Action.actions = function(params) {
	/**
	 * Show g_a_ajax_loading element
	 */
	$('#g_a_ajax_loading').show();
	$.ajaxq('core_action', {
		type: 'post',
		url: params.url,
		data: params,
		success: function(response) {
			if (response == 'RESULT_OK') {
				switch (params.act) {
					case 'add':
						var rel = 'delete_' + params.module_id  + '_' + params.controller_id + '_' + params.action_id;
						var message = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('action_actions_add_success')); ?>';
						var className = 'delete';
						break;
					case 'delete':
						var rel = 'add_' + params.module_id  + '_' + params.controller_id + '_' + params.action_id;
						var message = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('action_actions_delete_success')); ?>';
						var className = 'add';
						break;
				}
				$(Gio.Core.Action.currenAction).attr('rel', rel)
												   .removeClass('delete')
												   .removeClass('add')
												   .addClass(className);
				Gio.Message.success(message);
			} else {
				Gio.Message.error('<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_error_message')); ?>');
			}
			/**
			 * Hide g_a_ajax_loading element
			 */
			$('#g_a_ajax_loading').hide();
		}
	});
};

$(document).ready(function() {
	$('.g_a_controllers').each(function() {
		var self = this;
		$(self).bind('click', function() {
			Gio.Core.Controller.currenAction = this;
			var stringId = $(this).attr('rel');
			var temp = stringId.split('_');
			
			var act = temp[0];
			var module_id = temp[1];
			var controller_id = temp[2];
			var url = ''; 
			switch (act) {
				case 'delete':
					url = '<?php echo $this->url('core_controller_delete'); ?>';
					var messsage_confirm = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('controller_actions_delete_confirm')); ?>';
					break;
				case 'add':
					url = '<?php echo $this->url('core_controller_add'); ?>';
					var messsage_confirm = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('controller_actions_add_confirm')); ?>';
					break;
			}
			data = { url: url, module_id: module_id, controller_id: controller_id, act: act };
			Gio.Message.yesButton = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_yes_button', 'core')); ?>';
			Gio.Message.noButton = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_no_button', 'core')); ?>';
			Gio.Message.confirm(messsage_confirm, 'Gio.Core.Controller.actions', data);
		});
	});	

	$('.g_a_controllers_actions').each(function() {
		var self = this;
		$(self).bind('click', function() {
			Gio.Core.Action.currenAction = this;
			var stringId = $(this).attr('rel');
			var temp = stringId.split('_');
			
			var act = temp[0];
			var module_id = temp[1];
			var controller_id = temp[2];
			var action_id = temp[3];
			var url = ''; 
			switch (act) {
				case 'delete':
					url = '<?php echo $this->url('core_action_delete'); ?>';
					var messsage_confirm = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('action_actions_delete_confirm')); ?>';
					break;
				case 'add':
					url = '<?php echo $this->url('core_action_add'); ?>';
					var messsage_confirm = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('action_actions_add_confirm')); ?>';
					break;
			}
			data = { url: url, module_id: module_id, controller_id: controller_id, action_id: action_id, act: act };
			Gio.Message.yesButton = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_yes_button', 'core')); ?>';
			Gio.Message.noButton = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_no_button', 'core')); ?>';
			Gio.Message.confirm(messsage_confirm, 'Gio.Core.Action.actions', data);
		});
	});
});
</script>
<?php else : ?>
<div class="g_a_data_notfound"><?php echo $this->TRANSLATOR->translator('common_data_notfound'); ?></div>
<?php endif; ?>
<div class="clear"></div>